package com.bdqn.zhang.dao;

import com.bdqn.zhang.dto.CatDto;
import com.bdqn.zhang.dto.CatProjection;
import com.bdqn.zhang.pojo.Cat;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;

public interface CatRepository extends JpaRepository<Cat, Integer> {

    @Query(" SELECT MAX(c.age) FROM Cat c ")
    public Long maxAge();


    @Query("select c from Cat c where c.name like %:name% ")
    List<Cat> queryByname(@Param(value = "name") String name);


    //----------------------------以下是自定义条件查询--------------------------------------
    //Like --- 等价于 SQL 中的 "like"，比如 findByNameLike(String name);
    public List<Cat> findByNameLike(String name);


    //Like --- 等价于 SQL 中的 "like"，比如 findByNameLike(String name);
    public List<Cat> findByNameLike(String name, Sort sort);
    public Page<Cat> findByNameLike(String name, Pageable pageable);

    // **************** 原生SQL********************
    @Query(value=" SELECT * FROM cattb WHERE NAME LIKE %:name% ",nativeQuery=true)
    List<Cat> queryBynameSQL(@Param(value = "name") String name);

    //投影查询
    @Query("select  c.name , c.birthday from Cat c where id = ?1")
    Collection<CatProjection> findCatNameAndBirthdayById(Integer id);

    //投影查询
    @Query("select  new com.bdqn.zhang.dto.CatDto(c.name , c.birthday) from Cat c where id = ?1")
    Collection<CatDto> findCatNameAndBirthdayById2(Integer id);
}

